let result = [];
let count = 0;

function mapChildren(children, func, context) {
  //TODO实现此mapChildren方法
  children.map((child, i) => {
    invoke(child, count, func);
    count++;
  });
  return result;
}

function invoke(child, count, func) {
  if (!child) {
    return null;
  }
  if (Array.isArray(child)) {
    child.map((subChild, i) => {
      subChild.count = i;
      invoke(subChild, count, func);
    });
  } else {
    let tempRes = [];
    if (child.count) {
      tempRes = func(child, count + child.count);
    } else {
      tempRes = func(child, count);
    }
    if (tempRes && tempRes.length) {
      tempRes.map((subRes) => {
        result.push(subRes);
      });
    }
  }
}
export { mapChildren as map };
